<template>
  <div id="app">
    <keep-alive>
        <router-view v-if="$route.meta.keepAlive"></router-view>
    </keep-alive>
    <router-view v-if="!$route.meta.keepAlive"></router-view>
    

    <page-loading :isShow='showPageLoading' />
    <Tabbar v-if="tabbarShow" :curSelect='selectNum'/>

    <networkout v-if='networkStatus' />
  </div>
</template>

<script>
import networkout from '@/components/networkOut';
import Tabbar from "@/components/fixTabbar";
import pageLoading from '@/components/pageLoading';
import tool from '@/assets/js/tool.js';
import update from '@/assets/js/update.js'

export default {
  name: "App",
  provide(){
    return {
      reload:this.reload
    }
  },
  data: function(){
    return {
      isRouterAlive: true,
      tabbarShow:true,
      selectNum:null,
      networkStatus: false
    }
  },
  components: {
    Tabbar,
    pageLoading,
    networkout
  },
  created:function(){
    this.judegeRefresh();
    // 提交mutation中设置uid的方法
    this.$store.dispatch('setUserID')
    // 检查更新
    let that = this;
    document.addEventListener("plusready", function() {
      update.updateVersion(that, window)
    }, false);
    document.addEventListener("netchange", function () { //监听网络变化事件
      // 延迟二秒获取网络状态
      setTimeout( () => {
        let target = plus.networkinfo.getCurrentType();
        // 0 表示不认识的网络。 1表示未连接网络
        if ( target == 0 || target == 1) {
          console.log('无网络连接',target)
          that.networkStatus = true;
          that.$store.commit('changeNetworkStatus',true);
        }else{
          that.networkStatus = false;
          that.$store.commit('changeNetworkStatus',false);
        }
      },2000)
    }, false);
  },
  mounted:function(){
    tool.setBarStyle('#fff','dark');
  },
  watch: {
    //监听路由的变化
    $route(to, from) {
      //判断是否显示tabbar
      if (to.path == "/" || to.path == "/index" || (to.path == "/askTeacher" && !this.$route.query.isMiddlePage) || to.path == '/myCenter') {
        this.tabbarShow = true;
      } else {
        this.tabbarShow = false;
      }
      this.judegeRefresh();
    }
  },
  computed:{
    //是否显示整个加载页面
    showPageLoading:function(){
      return this.$store.state.pageLoading;
    }
  },
  methods:{
    //判断当前在哪个页面，用于刷新页面时，图标位置显示不正确的问题。
    judegeRefresh:function(){
      if (this.$route.path == "/" || this.$route.path == "/index" ) {
        this.selectNum = 1;
      } else if( this.$route.path == "/askTeacher" ){
        this.selectNum = 2;
      }else if( this.$route.path == '/myCenter' ){
        this.selectNum = 3;
      }
    }
  }
};
</script>

<style lang='less'>
  @import './assets/css/reset.less';
  // #app{
  //   min-height: 100%;
  // }
</style>
